// pages/findOrganization/index.js
const util = require('../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {

    contentActive: 'org1', // 内容栏选中id
    navActive: 0,
    heightArr: [],
    containerH: 0,
    org_typelist: [{
        _id: 'org1',
        type: '组织',
        itemlist: []
      },
      {
        _id: 'org2',
        type: '学生会',
        itemlist: []
      },
      {
        _id: 'org3',
        type: '社团',
        itemlist: []
      },
      {
        _id: 'org4',
        type: '创新创业项目',
        itemlist: []
      },
      {
        _id: 'org5',
        type: '其他',
        itemlist: []
      }
    ],
    is_manage: false
  },
  toOrgform: function (e) {
    wx.navigateTo({
      url: `/pages/OrganDetail/index?id=${e.currentTarget.dataset.id}`,
    })

  },
  addnewOrg: function () {
    wx.navigateTo({
      url: '/pages/OrganForm/index',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.getOrglist()
    let is_manage = wx.getStorageSync('is_manage')
    if (is_manage === '') {
      let openid = wx.getStorageSync('OPENID')
      wx.cloud.database().collection('users').where({
        _openid: openid
      }).get({
        success: res => {
          is_manage = res.data[0].manage
          this.setData({
            is_manage: is_manage
          })
          wx.setStorageSync('is_manage', is_manage)
        }
      })
    }
    this.setData({
      is_manage: is_manage
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: async function () {

  },
  getheightArr: function () {
    let query = wx.createSelectorQuery().in(this)
    let heightArr = []
    let s = 0;
    query.selectAll('.org_card').boundingClientRect((react) => {
      console.log(react)
      react.forEach((res) => {
        s += res.height;
        heightArr.push(s - 150)
      })
      this.setData({
        heightArr: heightArr
      })
    })
    query.select('.content').boundingClientRect((res) => {
      this.setData({
        containerH: res.height
      })
    }).exec()
  },
  onScroll(e) {
    let scrollTop = e.detail.scrollTop
    let scrollArr = this.data.heightArr
    if (scrollTop >= scrollArr[scrollArr.length - 1] - this.data.containerH) {
      return
    } else {

      for (let i = 0; i < scrollArr.length; i++) {
        if (scrollTop >= 0 && scrollTop < scrollArr[0]) {
          this.setData({
            navActive: 0
          })
        } else if ((scrollTop + 100) >= scrollArr[i - 1] && scrollTop < scrollArr[i]) {
          this.setData({
            navActive: i
          })
        }
      }
    }
  },
  getOrglist: async function (){
    await wx.cloud.database().collection('orglist').get({
      success: (res) => {
        console.log(res)
        let org_typelist = this.data.org_typelist
        org_typelist[0].itemlist = res.data.filter((item) => {
          item.intro = util.subStrFormat(item.org_intro, 10)
          return item.type == '组织'
        })
        org_typelist[1].itemlist = res.data.filter((item) => {
          item.intro = util.subStrFormat(item.org_intro, 13)
          return item.type == '学生会'
        })
        org_typelist[2].itemlist = res.data.filter((item) => {
          item.intro = util.subStrFormat(item.org_intro, 13)
          return item.type == '社团'
        })
        org_typelist[3].itemlist = res.data.filter((item) => {
          item.intro = util.subStrFormat(item.org_intro, 13)
          return item.type == '创新创业项目'
        })
        org_typelist[4].itemlist = res.data.filter((item) => {
          item.intro = util.subStrFormat(item.org_intro, 13)
          return item.type == '其他'
        })
        this.setData({
          org_typelist: org_typelist
        })
        this.getheightArr()
      }
    })
  },
  chooseType(e) {
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    this.setData({
      contentActive: id,
      navActive: index
    })
  },
  changelike(e) {
    let index = e.currentTarget.dataset.index
    let typeindex = e.currentTarget.dataset.typeindex
    let like = e.currentTarget.dataset.like
    let org_typelist = this.data.org_typelist
    org_typelist[typeindex].itemlist[index].like = !like
    this.setData({
      org_typelist: org_typelist
    })
  },
  onPullDownRefresh: function () {

    this.getOrglist()
    wx.stopPullDownRefresh()
  }
})